package com.epam.Parsers;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.log4j.Logger;

import com.epam.Xml.Hotel;
import com.epam.Xml.TouristVoucher;

public class StAXParser {

	private static List<TouristVoucher> touristVouchers;
	private static Logger log = Logger.getLogger(StAXParser.class);
	private static TouristVoucher voucher;
	private static InputStream is = StAXParser.class.getClassLoader().getResourceAsStream("NewFile.xml");
	private static String tagContent = null;
	private static Hotel hotel;

	public static List<TouristVoucher> parseXML() {
					
			try {
				XMLInputFactory inf = XMLInputFactory.newInstance();
				XMLStreamReader reader = inf.createXMLStreamReader(is);
				while (reader.hasNext()) {
					int event = reader.next();

					switch (event) {
					case XMLStreamConstants.START_ELEMENT:
						if ("Vouchers".equals(reader.getLocalName())) {
							touristVouchers = new LinkedList<TouristVoucher>();
						}
						if ("voucher".equals(reader.getLocalName())) {
							voucher = new TouristVoucher();
							voucher.setId(Integer.parseInt(reader
									.getAttributeValue("", "id")));
						}
						if ("hotel".equals(reader.getLocalName())) {
							hotel = new Hotel();
						}
						break;

					case XMLStreamConstants.CHARACTERS:
						tagContent = reader.getText().trim();
						break;

					case XMLStreamConstants.END_ELEMENT:
						switch (reader.getLocalName()) {
						case "voucher":
							touristVouchers.add(voucher);
							break;
						case "hotel":
							voucher.setHotel(hotel);
							break;
						case "cost":
							voucher.setCost(Double.parseDouble(tagContent));
							break;
						case "country":
							voucher.setCountry(Enum.valueOf(
									TouristVoucher.Country.class, tagContent));
							break;
						case "duration":
							voucher.setDuration(Integer.parseInt(tagContent));
							break;
						case "transport":
							voucher.setTransport(Enum.valueOf(
									TouristVoucher.Transport.class, tagContent));
							break;
						case "type":
							voucher.setType(Enum.valueOf(TouristVoucher.Type.class,
									tagContent));
							break;
						case "name":
							hotel.setName(tagContent);
							break;
						case "stars":
							hotel.setStars(Integer.parseInt(tagContent));
							break;
						case "capacity":
							hotel.setCapacity(Integer.parseInt(tagContent));
							break;
						case "tv":
							hotel.setTv(Boolean.parseBoolean(tagContent));
							break;
						case "conditioning":
							hotel.setConditioning(Boolean.parseBoolean(tagContent));
							break;
						case "feeding":
							hotel.setFeeding(Enum.valueOf(Hotel.Feeding.class,
									tagContent));
							break;

						}
					}

				}
			} catch (NumberFormatException | XMLStreamException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return touristVouchers;

	}

}
